package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpWarehousingInfo;
import org.dromara.erp.mapper.ErpWarehousingInfoMapper;
import org.dromara.erp.service.IErpWarehousingInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 入库明细Service业务层处理
 *
 * @author Mr Lei
 * @date 2024-06-05
 */
@RequiredArgsConstructor
@Service
public class ErpWarehousingInfoServiceImpl implements IErpWarehousingInfoService {

    private final ErpWarehousingInfoMapper baseMapper;

    /**
     * 查询入库明细
     */
    @Override
    public ErpWarehousingInfo queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询入库明细列表
     */
    @Override
    public TableDataInfo<ErpWarehousingInfo> queryPageList(ErpWarehousingInfo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpWarehousingInfo> lqw = buildQueryWrapper(bo);
        Page<ErpWarehousingInfo> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询入库明细列表
     */
    @Override
    public List<ErpWarehousingInfo> queryList(ErpWarehousingInfo bo) {
        LambdaQueryWrapper<ErpWarehousingInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpWarehousingInfo> buildQueryWrapper(ErpWarehousingInfo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpWarehousingInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, ErpWarehousingInfo::getOrderId, bo.getOrderId());
        lqw.eq(bo.getCateId() != null, ErpWarehousingInfo::getCateId, bo.getCateId());
        lqw.eq(bo.getProductId() != null, ErpWarehousingInfo::getProductId, bo.getProductId());
        lqw.eq(bo.getAmount() != null, ErpWarehousingInfo::getAmount, bo.getAmount());
        lqw.eq(bo.getMoney() != null, ErpWarehousingInfo::getMoney, bo.getMoney());
        return lqw;
    }

    /**
     * 新增入库明细
     */
    @Override
    public Boolean insert(ErpWarehousingInfo bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改入库明细
     */
    @Override
    public Boolean update(ErpWarehousingInfo bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpWarehousingInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除入库明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
